package com.glory.nicleanrpt

import com.glory.njcleanrpt.DateUtil
import com.glory.njcleanrpt.TranposUtils
import com.ivan.utils.PathUtil

import java.text.SimpleDateFormat

/**
 * @Program: njcleanrpt
 * @Description: 收发货在制品状况
 * @Author: ivan
 */
class ManufactureController {

    def dynamicQueryService

    /**
     * 收发货在制品状况(一阶)
     * @return
     */
    def manufacture() {
        request.setCharacterEncoding("UTF-8")
        response.setContentType("text/html;charset=UTF-8")
        params.startTime = DateUtil.getStartDate(params.startTime).startDate
        params.endTime = DateUtil.getEndDate(params.endTime).endDate
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd")
        List<String> datesList = DateUtil.getBetweenDates(sdf.parse(params.startTime),sdf.parse(params.endTime))
        if (datesList) {
            datesList.add(params.startTime)
            datesList.add(params.endTime)
        }
        def dateList = []
        def customerRecGoodsList = []
        def customerWipList = []
        def customerDelGoodsList = []
        def projectRecGoodsList = []
        def projectWipList = []
        def totalList = []
        def sysQueryPath = PathUtil.getPath(this, request)
        if ('Excel'.equals(params.EXPORT_CONTENT_NAME)) {
            params.SYS_QUERY_NAME = 'dateList'
            dateList = dynamicQueryService.queryForList(params)
            // 客户来货
            params.SYS_QUERY_NAME = 'customerRecGoodsList'
            customerRecGoodsList = dynamicQueryService.queryForList(params)
            customerRecGoodsList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', customerRecGoodsList)
            // 客户在制品
            params.SYS_QUERY_NAME = 'customerWipList'
            customerWipList = dynamicQueryService.queryForList(params)
            customerWipList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', customerWipList)
            // 客户发货
            params.SYS_QUERY_NAME = 'customerDelGoodsList'
            customerDelGoodsList = dynamicQueryService.queryForList(params)
            customerDelGoodsList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', customerDelGoodsList)

            // 项目来货 课别+部门
            params.SYS_QUERY_NAME = 'projectRecGoodsList'
            projectRecGoodsList = dynamicQueryService.queryForList(params)
            projectRecGoodsList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', projectRecGoodsList)
            // 项目在制品 课别+部门
            params.SYS_QUERY_NAME = 'projectWipList'
            projectWipList = dynamicQueryService.queryForList(params)
            projectWipList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', projectWipList)
            // 总体状况
            params.SYS_QUERY_NAME = 'totalList'
            totalList = dynamicQueryService.queryForList(params)
            totalList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', totalList)
        }
        [
                startTime           : params.startTime ?: '',
                endTime             : params.endTime ?: '',
                customName          : params.customName ?: '',
                deviceName          : params.deviceName ?: '',
                partName            : params.partName ?: '',
                markNum             : params.markNum ?: '',
                datesList           : datesList,
                dateList            : dateList,
                customerRecGoodsList: customerRecGoodsList,
                customerWipList     : customerWipList,
                customerDelGoodsList: customerDelGoodsList,
                projectRecGoodsList : projectRecGoodsList,
                projectWipList      : projectWipList,
                totalList           : totalList,
                SYS_QUERY_PATH      : sysQueryPath
        ]
    }

    /**
     * 收发货在制品状况明细
     * @return
     */
    def manufactureDetail() {
        request.setCharacterEncoding("UTF-8")
        response.setContentType("text/html;charset=UTF-8")
        params.startTime = DateUtil.getStartDate(params.startTime).startDate
        def pageCurrent = params.pageCurrent ? Integer.parseInt(params.pageCurrent) : 1
        def pageSize = params.pageSize ? Integer.parseInt(params.pageSize) : 100
        params.startno = (pageSize * (pageCurrent - 1)) + 1
        params.endno = pageSize * pageCurrent
        def pageSizeList = [10, 20, 50, 100]
        def reportList = []
        def tableHeadList = []
        def totalCount = 0
        def sysQueryName
        if ('customerRecGoods'.equals(params.flag)) {
            // 客户来货
            params.SYS_QUERY_NAME = 'customerRecGoodsDetailList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            params.SYS_QUERY_NAME = 'customerRecGoodsDetailCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        } else if ('customerWip'.equals(params.flag)) {
            // 客户在制品
            params.SYS_QUERY_NAME = 'customerWipDetailList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            params.SYS_QUERY_NAME = 'customerWipDetailCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        } else if ('customerDelGoods'.equals(params.flag)){
            // 客户发货
            params.SYS_QUERY_NAME = 'customerDelGoodsDetailList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            params.SYS_QUERY_NAME = 'customerDelGoodsDetailCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        } else if ('projectRecGoods'.equals(params.flag)){
            // 课别+部门来货
            params.SYS_QUERY_NAME = 'projectRecGoodsDetailList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            params.SYS_QUERY_NAME = 'projectRecGoodsDetailCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        } else if ('projectWip'.equals(params.flag)){
            // 课别+部门在制品
            params.SYS_QUERY_NAME = 'projectWipDetailList'
            reportList = dynamicQueryService.queryForList(params)
            sysQueryName = params.SYS_QUERY_NAME
            if (reportList.size() > 0) {
                tableHeadList = reportList[0].keySet()
            }
            params.SYS_QUERY_NAME = 'projectWipDetailCount'
            totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
        } else if ('total'.equals(params.flag)) {
            // 总体状况
            if ('LAIHUO'.equals(params.clazz)) {
                params.SYS_QUERY_NAME = 'totalRecGoodsDetailList'
                reportList = dynamicQueryService.queryForList(params)
                sysQueryName = params.SYS_QUERY_NAME
                if (reportList.size() > 0) {
                    tableHeadList = reportList[0].keySet()
                }
                params.SYS_QUERY_NAME = 'totalRecGoodsDetailCount'
                totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
            } else if ('FAHUO'.equals(params.clazz)) {
                params.SYS_QUERY_NAME = 'totalDelGoodsDetailList'
                reportList = dynamicQueryService.queryForList(params)
                sysQueryName = params.SYS_QUERY_NAME
                if (reportList.size() > 0) {
                    tableHeadList = reportList[0].keySet()
                }
                params.SYS_QUERY_NAME = 'totalDelGoodsDetailCount'
                totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
            } else if ('WIP'.equals(params.clazz)) {
                params.SYS_QUERY_NAME = 'totalWipDetailList'
                reportList = dynamicQueryService.queryForList(params)
                sysQueryName = params.SYS_QUERY_NAME
                if (reportList.size() > 0) {
                    tableHeadList = reportList[0].keySet()
                }
                params.SYS_QUERY_NAME = 'totalWipDetailCount'
                totalCount = dynamicQueryService.queryForList(params)[0].TOTALCOUNT
            }
        }
        [
                pageCurrent   : pageCurrent,
                pageSize      : pageSize,
                pageSizeList  : pageSizeList,
                startno       : params.startno ?: '',
                endno         : params.endno ?: '',
                startTime     : params.startTime ?: '',
                clazz         : params.clazz ?: '',
                flag          : params.flag ?: '',
                tableHeadList : tableHeadList,
                reportList    : reportList,
                totalCount    : totalCount,
                SYS_QUERY_NAME: sysQueryName
        ]
    }

    /**
     * 一阶导出Excel
     * @return
     */
    def exportExcel() {
        params.startno = 1
        params.endno = 99999999999999999
        params.SYS_QUERY_NAME = 'dateList'
        def dateList = dynamicQueryService.queryForList(params)
        // 客户来货
        params.SYS_QUERY_NAME = 'customerRecGoodsList'
        def customerRecGoodsList = dynamicQueryService.queryForList(params)
        customerRecGoodsList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', customerRecGoodsList)
        // 客户在制品
        params.SYS_QUERY_NAME = 'customerWipList'
        def customerWipList = dynamicQueryService.queryForList(params)
        customerWipList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', customerWipList)
        // 客户发货
        params.SYS_QUERY_NAME = 'customerDelGoodsList'
        def customerDelGoodsList = dynamicQueryService.queryForList(params)
        customerDelGoodsList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', customerDelGoodsList)
        // 项目来货 课别+部门
        params.SYS_QUERY_NAME = 'projectRecGoodsList'
        def projectRecGoodsList = dynamicQueryService.queryForList(params)
        projectRecGoodsList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', projectRecGoodsList)
        // 项目在制品 课别+部门
        params.SYS_QUERY_NAME = 'projectWipList'
        def projectWipList = dynamicQueryService.queryForList(params)
        projectWipList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', projectWipList)
        // 总体状况
        params.SYS_QUERY_NAME = 'totalList'
        def totalList = dynamicQueryService.queryForList(params)
        totalList = TranposUtils.colToRow('TRANS_TIME', 'MAIN_QTY', totalList)
        String filename = new String("shoufahuowip.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(
                template: "export${params.EXPORT_CONTENT_NAME}",
                model: [
                        dateList            : dateList,
                        customerRecGoodsList: customerRecGoodsList,
                        customerWipList     : customerWipList,
                        customerDelGoodsList: customerDelGoodsList,
                        projectRecGoodsList : projectRecGoodsList,
                        projectWipList      : projectWipList,
                        totalList           : totalList
                ]
        )
    }
}